package com.example.lotterysystem.service.impl;

import com.example.lotterysystem.common.errorcode.ServiceErrorCodeConstants;
import com.example.lotterysystem.common.exception.ServiceException;
import com.example.lotterysystem.controller.param.CreatePrizeParam;
import com.example.lotterysystem.controller.param.PageParam;
import com.example.lotterysystem.controller.param.UpdatePrizeParam;
import com.example.lotterysystem.dao.dataobject.PrizeDO;
import com.example.lotterysystem.dao.mapper.ActivityMapper;
import com.example.lotterysystem.dao.mapper.ActivityPrizeMapper;
import com.example.lotterysystem.dao.mapper.PrizeMapper;
import com.example.lotterysystem.service.PictureService;
import com.example.lotterysystem.service.PrizeService;
import com.example.lotterysystem.service.dto.PageListDTO;
import com.example.lotterysystem.service.dto.PrizeDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

@Service
public class PrizeServiceImpl implements PrizeService {
    @Autowired
    PictureService pictureService;

    @Autowired
    private PrizeMapper prizeMapper;

    @Autowired
    private ActivityPrizeMapper activityPrizeMapper;

    /**
     * 创建单个奖品
     * @param param 奖品属性
     * @param picFile 奖品图片
     * @return
     */
    @Override
    public Long createPrize(CreatePrizeParam param, MultipartFile picFile) {
        //上传图片
        String fileName = pictureService.savePicture(picFile);

        //存库
        PrizeDO prizeDO = new PrizeDO();
        prizeDO.setName(param.getPrizeName());
        prizeDO.setDescription(param.getDescription());
        prizeDO.setPrice(param.getPrice());
        prizeDO.setImageUrl(fileName);
        prizeMapper.insert(prizeDO);

        return prizeDO.getId();
    }

    @Override
    public Boolean updatePrize(UpdatePrizeParam param, MultipartFile picFile) {
        //上传图片
        String fileName = null;
        if(picFile != null) {
            fileName = pictureService.savePicture(picFile);
        }

        //存库
        PrizeDO prizeDO = new PrizeDO();
        prizeDO.setId(param.getPrizeId());
        prizeDO.setName(param.getPrizeName());
        prizeDO.setDescription(param.getDescription());
        prizeDO.setPrice(param.getPrice());
        prizeDO.setImageUrl(fileName);

        Integer result = prizeMapper.update(prizeDO);
        return result > 0;
    }

    /**
     * 翻页查询列表
     * @param param
     * @return
     */
    @Override
    public PageListDTO<PrizeDTO> findPrizeList(PageParam param , String prizeName) {
        if(prizeName == null || prizeName.length() == 0) {
            prizeName = null;
        }
        //总量
        int total = prizeMapper.count(prizeName);

        //查询当前页列表
        List<PrizeDTO> prizeDTOList = new ArrayList<>();
        List<PrizeDO> prizeDOList = prizeMapper.selectPrizeList(param.offset(), param.getPageSize(), prizeName);

        for(PrizeDO prizeDO : prizeDOList) {
            PrizeDTO prizeDTO = new PrizeDTO();
            prizeDTO.setPrizeId(prizeDO.getId());
            prizeDTO.setName(prizeDO.getName());
            prizeDTO.setPrice(prizeDO.getPrice());
            prizeDTO.setDescription(prizeDO.getDescription());
            prizeDTO.setImageUrl(prizeDO.getImageUrl());
            prizeDTOList.add(prizeDTO);
        }
        return new PageListDTO<>(total, prizeDTOList);
    }

    @Override
    public Boolean deletePrize(Long prizeId) {
        if(prizeId == null) {
            return false;
        }
        //统计奖品被活动关联的次数
        int count = activityPrizeMapper.countActivityPrize(prizeId);
        if(count > 0) {
            throw new ServiceException(ServiceErrorCodeConstants.PRIZE_IS_LINKED_TO_ACTIVITY);
        }
        //删除奖品
        int result = prizeMapper.deleteByPrizeId(prizeId);
        return result > 0;
    }

    @Override
    public PrizeDO getPrizeInfo(Long prizeId) {
        PrizeDO prizeDO = prizeMapper.selectById(prizeId);
        return prizeDO;
    }


}
